﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace Jzy.Workflow.Code.Menu
{
    public class MenuItem
    {
        public string Text
        {
            get
            {
                return _lbl.Text;
            }
            set
            {
                _lbl.Text = value;
            }
        }

        private Rectangle _rec;
        private TextBlock _lbl;
        private Button _btn;

        public event RoutedEventHandler Click;

        public MenuItem()
        {
            _rec = new Rectangle();
            _rec.Width = 114;
            Grid.SetColumnSpan(_rec, 2);
            Grid.SetColumn(_rec, 0);

            _lbl = new TextBlock();
            _lbl.VerticalAlignment = VerticalAlignment.Center;
            _lbl.HorizontalAlignment = HorizontalAlignment.Left;
            Grid.SetColumn(_lbl, 1);

            _btn = new Button();
            _btn.Opacity = 0;
            Grid.SetColumn(_btn, 0);
            Grid.SetColumnSpan(_btn, 2);
            _btn.MouseEnter += new MouseEventHandler(_btn_MouseEnter);
            _btn.MouseLeave += new MouseEventHandler(_btn_MouseLeave);
            _btn.Click += new RoutedEventHandler(_btn_Click);
            Click += new RoutedEventHandler(MenuItem_Click);
        }

        public MenuItem(string text):this()
        {
            this.Text = text;
        }

        void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            
        }

        void _btn_Click(object sender, RoutedEventArgs e)
        {
            Click(this, e);            
        }

        public void Render(Grid grid, int index)
        {
            Grid.SetRow(_rec, index);
            Grid.SetRow(_lbl, index);
            Grid.SetRow(_btn, index);

            grid.Children.Add(_rec);
            grid.Children.Add(_lbl);
            grid.Children.Add(_btn);
        }

        void _btn_MouseLeave(object sender, MouseEventArgs e)
        {
            _rec.Fill = null;
        }

        void _btn_MouseEnter(object sender, MouseEventArgs e)
        {
            ImageBrush brush = new ImageBrush();
            brush.ImageSource = new BitmapImage(new Uri("images/select_menu_bg.png", UriKind.Relative));
            _rec.Fill = brush;
        }
    }
}
